﻿@page "/mud/project/releases"
@using System.Text.RegularExpressions

@inject GitHubApiClient _gitHubApiClient;

<DocsPage DisplayFooter="true">
    <DocsPageHeader Title="Releases" />
    <DocsPageContent>
        @if (_githubReleases == null)
        {
            <MudProgressCircular Color="Color.Primary" Indeterminate="true" />
        }
        else
        {
            <MudGrid>
                @foreach (var release in _githubReleases)
                {
                    <MudItem xs="12" md="4">
                        <div class="docs-sticky-info">
                            <MudText Typo="Typo.h5" Inline="true">Version </MudText>
                            <MudText Typo="Typo.h5" Inline="true" Color="Color.Primary"><b>@release.name</b></MudText>
                            <MudText Typo="Typo.subtitle1">Released on @release.published_at.ToString("MMMM MM, yyyy")</MudText>
                        </div>
                    </MudItem>
                    <MudItem xs="12" md="8">
                        <div class="docs-github-release-body">
                            @((MarkupString)GetBody(release.body))
                        </div>
                    </MudItem>
                }
        </MudGrid>
        }
    </DocsPageContent>
</DocsPage>

@code {

    private GitHubReleases[] _githubReleases;
    
    protected override async Task OnInitializedAsync()
    {
        _githubReleases = await _gitHubApiClient.GetReleasesAsync();
        StateHasChanged();
    }

    private string GetBody(string value)
    {
        string output = value;
        output = Regex.Replace(output, @"(^|\n)# .+", "<h5 class=\"mud-typography mud-typography-h5 mud-inherit-text\">$&</h5>");
        output = Regex.Replace(output, @"(^|\n)## .+", "<h5 class=\"mud-typography mud-typography-h5 mud-inherit-text\">$&</h5>");
        output = Regex.Replace(output, @"(^|\n)### .+", "<h6 class=\"mud-typography mud-typography-h6 mud-inherit-text\">$&</h6>");
        output = Regex.Replace(output, @"(#){1}([0-9]{4}){1}", "<a target=\"_blank\" href=\"https://github.com/MudBlazor/MudBlazor/pull/$2\">#$2</a>");
        output = output.Replace("- **", "<b class=\"mx-3\">•</b><b>").Replace("**:",":</b>");
        output = output.Replace("### ", "").Replace("## ","").Replace("# ","#");
        output = output.Replace(">#MudBlazor", ">MudBlazor");
        return output;
    }

}